﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace __Able.Network.Connection
{
    public class ConnectionPool : IConnectionPool
    {
        /// <summary>
        /// 
        /// </summary>
        private Dictionary<string, IConnection> registry;
        /// <summary>
        /// 
        /// </summary>
        public ConnectionPool()
        {
            registry = new Dictionary<string, IConnection>();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="policy"></param>
        /// <param name="server"></param>
        /// <param name="connect"></param>
        /// <returns></returns>
        public IConnection NewTCPConnection(ConnectionPolicy policy, EndPoint server, bool connect)
        {
            IConnection conn = new TCPConnection(policy, server);
            if (connect)
            {
                //如果网络连接失败，则返回null
                if (!conn.Connect())
                    return null;
            }
            registry.Add(policy.connectId, conn);
            return conn;
        }
        /// <summary>
        /// 释放指定连接
        /// </summary>
        /// <param name="conn"></param>
        public void Release(IConnection conn)
        {
            lock (registry)
            {
                conn.Close();
                conn.Dispose();
                foreach (string name in registry.Keys)
                {
                    if (registry[name] == conn)
                    {
                        registry.Remove(name);
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 释放指定连接
        /// </summary>
        /// <param name="id"></param>
        public void Release(string id)
        {
            IConnection conn = GetConnection(id);
            if (conn != null)
            {
                Release(conn);
            }
        }
        /// <summary>
        /// 获取指定ID的连接
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IConnection GetConnection(string id)
        {
            if (registry.ContainsKey(id))
            {
                return registry[id];
            }
            return null;
        }
        /// <summary>
        /// 清除所有的连接并释放资源
        /// </summary>
        public void Dispose()
        {
            foreach (IConnection conn in registry.Values)
            {
                conn.Dispose();
            }
            registry.Clear();
        }
        /// <summary>
        /// 是否存在指定ID的连接
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasConnection(string id)
        {
            return registry.ContainsKey(id);
        }
        /// <summary>
        /// 获取所有的连接
        /// </summary>
        /// <returns></returns>
        public List<IConnection> GetConnections()
        {
            return new List<IConnection>(registry.Values);
        }
    }
}
